#!/usr/bin/env python3
#! -*- coding: utf-8 -*-

from functools import cmp_to_key
import re
from typing import Dict, List, Set
import requests
import os

# Path to rpm installplatform
INSTALL_PLATFORM_IN_URL = 'https://raw.githubusercontent.com/rpm-software-management/rpm/master/installplatform'
content = requests.get(INSTALL_PLATFORM_IN_URL).text
# with open(os.path.join(os.path.dirname(__file__), 'installplatform'), 'r', encoding='utf-8') as f:
#     content = f.read()

result_dic: Dict[str, Set[str]] = dict()

exp = r'ISANAME=(.*)\n\s*ISABITS=(.*)'
patt = re.compile(exp, re.I)
all_res = re.findall(patt, content)
name_maxlen = 0
for res in all_res:
    name, bits = res
    if name == '' or '`' in name or '$' in name:
        continue
    if name not in result_dic:
        result_dic[name] = set()
    result_dic[name].add(bits)
    name_maxlen = max(name_maxlen, len(name))

result_lis = list(result_dic.items())
result_lis.sort(key=lambda x: x[0])

final_res = '| `__isa_name` 宏的可能值 | 对应 `__isa_bits` 宏的可能值 | 对应 `_isa` 宏的可能值 |\n'
final_res += '| ---------------------- | --------------------------- | --------------------- |\n'
for name, all_bits in result_lis:
    all_bits_lis = list(all_bits)
    all_bits_lis.sort()
    all_comb = [f'({name}-{bits})' for bits in all_bits_lis]
    final_res += '| {}{} | `{}` | `{}` |\n'.format(
        name,
        ' ' * (name_maxlen - len(name)),
        '`, `'.join(all_bits_lis),
        '`, `'.join(all_comb)
    )
print(final_res)
